<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use manniu\controller\AdminBase;

use app\admin\model\Linkage as LinkageModel;
use app\admin\model\LinkageData as LinkageDataModel;

class Linkage extends AdminBase{

    //当前控制器菜单
    public $submenu = [];
	/**
	 * TP初始化方法
	 */
	public function _initialize(){
		parent::_initialize();
        $this->submenu[] = ['title' => '联动菜单列表', 'url' => url('admin/linkage/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '添加联动菜单', 'url' => url('admin/linkage/add'), 'icon' => '&#xe608;'];
        $this->submenu[] = ['title' => '更新缓存', 'url' => url('admin/linkage/cache'), 'icon' => 'ဂ'];
	}

    /**
     * 联动菜单列表
     * @access public
     * @return \think\Response
     */
    public function index(){
        $list = LinkageModel::order('id desc')->paginate(config('mn_admin.admin_pagesize'));
        $this->assign([
            'submenu' => $this->submenu,
            'list' => $list
        ]);
        return $this->fetch();
    }

    /**
     * 联动菜单增加
     * @access public
     * @return \think\Response
     */
    public function add(){
        // POST提交数据
        if($this->request->isPost()){
            // 实例化联动菜单模型
            $LinkageModel = new LinkageModel;
            // 获取提交数据
            $post_data = $this->buildParam();
            // 验证联动菜单名称是否重复
            $check_repeat = LinkageModel::where('name',$post_data['name'])->count();
            if($check_repeat){
                $this->error('联动菜单名称重复');
            }
            // 验证联动菜单唯一标识是否重复
            $check_repeat = LinkageModel::where('mark',$post_data['mark'])->count();
            if($check_repeat){
                $this->error('联动菜单唯一标识重复');
            }
            if($LinkageModel->allowField(true)->save($post_data)){
                $this->success('联动菜单添加成功','index');
            }else{
                $this->error($LinkageModel->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu
        ]);
        return $this->fetch();
    }

    /**
     * 更新缓存
     * @access public
     * @return void
     */
    public function cache(){
        $LinkageModel = new LinkageModel;
        $LinkageModel->cache();
        $this->success('更新缓存成功','index');
    }

    /**
     * 中国行政区域联动菜单导入
     * @access public
     * @return \think\Response
     */
    /*
    public function addChinasAd(){
        $chinas_administrative_division = json_decode(file_get_contents(ROOT_PATH.'manniu/static/chinas_administrative_division.json'),true);
        foreach ($chinas_administrative_division as $k1 => $v1) {
            $linkage_1 = new LinkageDataModel;
            $linkage_1->pid = 0;
            $linkage_1->name = $v1['name'];
            $linkage_1->linkage_id = 1;
            $linkage_1->save();
            foreach ($v1['child'] as $k2 => $v2) {
                $linkage_2 = new LinkageDataModel;
                $linkage_2->pid = $linkage_1->id;
                $linkage_2->name = $v2['name'];
                $linkage_2->linkage_id = 1;
                $linkage_2->save();
                foreach ($v2['child'] as $k3 => $v3) {
                    $linkage_3 = new LinkageDataModel;
                    $linkage_3->pid = $linkage_2->id;
                    $linkage_3->name = $v3;
                    $linkage_3->linkage_id = 1;
                    $linkage_3->save();
                }
            }
        }
        $this->success('导入成功','index');
    }
    */
}